package com.handu.luna.core.domain.article;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.handu.luna.core.domain.BasePersistentDomain;
import com.handu.luna.core.domain.search.ArticleSearch;


@Entity
public class ArticleList extends BasePersistentDomain<ArticleListRepository>{
	
	@NotNull(message="文章标题不能为空")
	@Size(min=1,max=100, message="文章标题长度超过限制！")
	private String title;  //文章标题
	private long catId; //分类ID
	
	private boolean articleType;  //文章重要性
	private boolean  displayed;   //isShow;   //是否显示


	

	@Min(value = 0, message = "取值范围:0~255,包含0,不包含255。") 
	@Max(value=254,message="最大不能超过255")
	private int sortOrder;  //排序
	
	
 
	private String keywords;  //关键字
	private String description;  //网页描述
	private  String link;  //外部链接
	private String fileUrl;   //上传文件url
	//private Date addTime;   //添加时间
	private int clickCount;  //点击次数
	
	@Lob
	private String content;   // 文章内容
	
	@Transient
	private ArticleCategory articleCategory;
	
	@Transient
	private String articleCategoryName;
	
	public ArticleCategory getArticleCategory() {
		return articleCategory;
	}
	public void setArticleCategory(ArticleCategory articleCategory) {
		this.articleCategory = articleCategory;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public long getCatId() {
		return catId;
	}
	public void setCatId(long catId) {
		this.catId = catId;
	}
	public boolean isArticleType() {
		return articleType;
	}
	public void setArticleType(boolean articleType) {
		this.articleType = articleType;
	}
	
	public int getSortOrder() {
		return sortOrder;
	}
	public void setSortOrder(int sortOrder) {
		this.sortOrder = sortOrder;
	}
	
	public boolean isDisplayed() {
		return displayed;
	}
	public void setDisplayed(boolean displayed) {
		this.displayed = displayed;
	}
	
	
	public String getKeywords() {
		return keywords;
	}
	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public String getLink() {
		return link;
	}
	public void setLink(String link) {
		this.link = link;
	}
	public String getFileUrl() {
		return fileUrl;
	}
	public void setFileUrl(String fileUrl) {
		this.fileUrl = fileUrl;
	}
	
	public int getClickCount() {
		return clickCount;
	}
	public void setClickCount(int clickCount) {
		this.clickCount = clickCount;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	
	public String getArticleCategoryName() {
		if(catId!=0){
			articleCategoryName=context.getArticleCategoryRepository().findOne(catId).getName();
		}
		return articleCategoryName;
	}
	public void setArticleCategoryName(String articleCategoryName) {
		this.articleCategoryName = articleCategoryName;
	}
	/**
	 * 多表查询的   初始化信息
	 */
	public void init(){
		
		articleCategory = context.getArticleCategoryRepository().findOne(catId);
	}
	
	public Page<ArticleList> findAll(Pageable pageable){
		return context.getArticleListRepository().findAll(pageable);
	}
	
	/**
	 * 点击文章分类，跳到相应的文章列表
	 * @param pageable
	 * @return
	 */
	public Page<ArticleList> findAll(long id,Pageable pageable){
		return context.getArticleListRepository().findAll(id,pageable);
	}
	
	public Page<ArticleList> findAll(ArticleSearch articleSearch,Pageable pageable){
		return context.getArticleListRepository().findAll(new ArticleList().selectCondition(articleSearch), pageable);
	}
	
	
	
	public ArticleList(){}
	
	public ArticleList(String title,long catId,boolean articleType,boolean displayed,int sortOrder,String keywords,String description,String link,String fileUrl,int clickCount,String content){
		super();
		this.title = title;
		this.catId = catId;
		this.articleType = articleType;
		this.displayed = displayed;
		this.sortOrder = sortOrder;
		
		
		this.keywords = keywords;
		this.description =description;
		this.link = link;
		this.fileUrl = fileUrl;
		//this.addTime = addTime;
		this.clickCount = clickCount;
		this.content = content;
	}
	
	public Specification<ArticleList> selectCondition(ArticleSearch articleSearch) {
		return new Specification<ArticleList>() {
			@Override
			public Predicate toPredicate(Root<ArticleList> root,
					CriteriaQuery<?> query, CriteriaBuilder builder) {

				Predicate predicate = builder.conjunction();
				List<Expression<Boolean>> expressions = predicate.getExpressions();
				
				//expressions.add(builder.not(root.get("deleted")));

//				if (NumberUtils.isDigits(articleSearch.getArticleCategoryId())) {
//					expressions.add(builder.equal(root.<Long> get("catId"),articleSearch.getArticleCategoryId())); // 分类id
//				}

			

				if (StringUtils.isNotBlank(articleSearch.getArticleTitle())) {
					expressions.add(builder.like(root.<String> get("title"), "%"+articleSearch.getArticleTitle() + "%")); // 文章标题
				}
				if(StringUtils.isNotBlank(articleSearch.getArticleCategoryId())){
					List<Long> ids = context.getArticleCategoryRepository().getAllArticleByParentCollection(articleSearch.getArticleCategoryId());
					
					ids.add(Long.parseLong(articleSearch.getArticleCategoryId()));
					
					expressions.add(root.<Long>get("catId").in(ids));
				}
				return query.where(predicate).getRestriction();

			}
		};
	}
	
}
